<%@page import="org.omg.CORBA.TIMEOUT"%>
<%@page import="databaseLayer.StatisticsLookUp" %>
<%@page import="databaseLayer.MoviesLookUp" %>
<%@page import="statistics.*" %>
<%@page import="movieBean.Movie" %>
<%@page import="movieBean.MovieSlot" %>

<script type="text/javascript" src="https://www.google.com/jsapi"></script>
    
    <% MovieStatistics[] mStatistics = StatisticsLookUp.getMovieStatistics();
       Movie[] movie = MoviesLookUp.getAllMovies("0000-00-00 00:00:00", "9999-99-99 23:59:59");
       int numberOfMovie;
       if(mStatistics != null)
           numberOfMovie = mStatistics.length;
       else
           numberOfMovie = 0;           
    %>
    <script type="text/javascript">
      google.load("visualization", "1", {packages:["corechart"]});
      google.setOnLoadCallback(drawChart);
      
      function drawChart() {
        var data = new Array();
        var options = new Array();
        var chart = new Array();
        <% for(int i=0; i<numberOfMovie; i++){ %>  
            data[<%= i %>] = google.visualization.arrayToDataTable(       
            [
            ['State', 'Amount'],
            ['Bought',     <%= mStatistics[i].getBought() %>],
            ['Available',      <%= mStatistics[i].getAvailable() %>],
            ['Unavailable',  <%= mStatistics[i].getUnavailable() %>],
            ]
            );

            options[<%= i %>]  = {
                title: '<%= movie[i].getName()%>'
            };
            
            chart[<%= i %>] = new google.visualization.PieChart(document.getElementById('chart_div' + [<%= i %>]));
            chart[<%= i %>].draw(data[<%= i %>], options[<%= i %>]);
        <% } %>
      }
</script>

<script type="text/javascript">
    function viewSlot(input){
        if(input == "0") {
            return;
        }
        var ticketingWindow = window.open("#", "CultCinema", "width=900,height=550,top=0,menubar=no,scrollbars=yes,status=yes");
        ticketingWindow.focus();
        ticketingWindow.location = "viewSlotStatistics.jsp?slotID="+input;
    }	
</script>

<% for(int i=0; i<numberOfMovie; i++){ %>
<div id="<%= "chart_div" + i %>" style="width: 900px; height: 500px;"></div>
<p>Duration: <%= movie[i].getDuration() %></p>
<p>Gross: <%= mStatistics[i].getGross() %></p>
<select name="slotID" onchange="viewSlot(this.options[this.selectedIndex].value)">
    <option value="0" selected="selected">Available Date and time</option>
    <% MovieSlot[] movieSlot = movie[i].getMovieSlot();
    for(int j=0; j<movieSlot.length; j++){ %>
    <option value="<%= movieSlot[j].getSlotID() %>&movieName=<%= movie[i].getName() %>&time=<%= movieSlot[j].getStartTime() %>"><%= movieSlot[j].getStartTime() %> House <%= movieSlot[j].getHouse() %> $<%= movieSlot[j].getPrice() %></option>
    <% } %>
</select>
<hr />
<% } %>